<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 使用el-tree组件展示主题内容 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="delAdd(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <el-dialog
        :title="isEdit? '编辑部门' : '添加部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <AddorEdit
          :id="curId"
          ref="myAdRef"
          :is-edit="isEdit"
          :origin="originList"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import AddorEdit from './deptDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      list: [], // 保存后台返回的数据部门列表
      showDialog: false, // 弹层的显示和隐藏
      curId: '', // 当前点击的部门id
      isEdit: false, // 编辑或者添加
      originList: [] // 获取所有部门的数据
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 获取部门列表信息
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, '获取部门信息列表')
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        res.data.depts.shift()
        // transDataToTreeData(res.data.depts) 把平铺数据转化树型数据
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增子部门
    hAdd(id) {
      this.curId = id
      this.isEdit = false // 添加
      this.showDialog = true
    },
    // 重新渲染 - 关闭弹出
    hSuccess() {
      this.loadDepartments()
      this.showDialog = false
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.isEdit = true // 编辑
      // 解决公共组件数据不更新的方式
      // 可以在父组件中调用子组件的获取部门详情的数据填充到表单
      this.$nextTick(() => {
        this.$refs.myAdRef.loadDepartmentsById()
      })
      this.showDialog = true
    },
    // 删除
    delAdd(id) {
      this.$confirm('您确认删除该信息吗').then(async() => {
        const res = await delDepartments(id)
        console.log('res删除该信息', res)
        this.$message.success(res.message)
        this.loadDepartments()
      }).catch(error => error)
    },
    // 清空表单
    hClose() {
      this.$refs.myAdRef.resetForm()
    }
  }
}
</script>
